﻿var page = 1;
var ue = UE.getEditor('content', {
    toolbars: [
        ['undo', 'redo', 'bold', 'emotion',
            'selectall', 'link', 'insertcode', 'fontfamily', 'fontsize']],
    enableAutoSave: false,
    maximumWords: 500,
    autoHeightEnabled: false,
    autoFloatEnabled: false,
    fontsize: UeditorFontSizeArray
});
var ue2 = UE.getEditor('dveditword', {
    toolbars: [
        ['fullscreen', 'source', 'undo', 'redo', 'bold', 'emotion',
            'selectall', 'insertcode', 'fontfamily', 'fontsize', 'simpleupload']],
    enableAutoSave: false,
    maximumWords: 500,
    autoHeightEnabled: false,
    autoFloatEnabled: false
});

function goSend() {
    if ($('#btnok').length == 0) {
        return;
    }
    var sh = $('#btnok').offset().top;
    $(window).scrollTop(sh);
    ue.focus(true);
}

function goReply(id, repuser) {
    if (id > 0) {
        $('#hID').val(id);
        ue.setContent('@' + repuser + '：');
    }
    goSend();
}

function showSendView(id, repuser) {
    if (id > 0) {
        $('#hID').val(id);
        ue.setContent('@' + repuser + '：');
    }
    $('body').css("overflow", "hidden");
    $('#dvBg').css("height", getScrollHeight() + "px").show();
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#dvSendView").height();
    var popupWidth = $("#dvSendView").width();
    $("#dvSendView").css({
        "top": (windowHeight - popupHeight - 200) / 2 + $(document).scrollTop() + 100,
        "left": (windowWidth - popupWidth) / 2
    }).show();
    ue.focus(true);
}

function clear() {
    ue.setContent('');
    $('#hID').val(0);
}
function showEdit() {
    $('#dvuserword').hide();
    $('#dvusertosay').show();
    if ($('#dvuserword').html() != '') {
        ue2.setContent($('#dvuserword').html());
    }
    ue2.focus(true);
}

function save() {
    $.post(blog_username + 'message/SaveUserWantSay', { wantsay: $.htmlEncode(ue2.getContent()) }, function (o) {
        if (o.State == 1) {
            $('#dvuserword').empty().html(ue2.getContent());
            $('#dvuserword').show();
            $('#dvusertosay').hide();
            ue2.setContent('');
            doUeParse();
        } else {
            layerAlert('保存失败：' + o.Message);
        }
    })
}

function cancel() {
    $('#dvuserword').show();
    $('#dvusertosay').hide();
    ue2.setContent('');
}

function delMsg(id, a) {
    layerConfirm('确定删除这条留言吗？', function () {
        $.post(blog_username + 'message/DelMsg', { id: id }, function (o) {
            if (o.State == 1) {
                layerMsg('删除成功！');
                getMess(page);
                changeCount(-1);
            } else {
                layerAlert('删除失败：' + o.Message);
            }
        })
    });
}

function delRpMsg(id, a) {
    layerConfirm('确定删除这条回复吗？', function () {
        $.post(blog_username + 'message/DelRpMsg', { id: id }, function (o) {
            if (o.State == 1) {
                layerMsg('删除成功！');
                getMess(page);
            } else {
                layerAlert('删除失败：' + o.Message);
            }
        })
    });
}

$(function () {
    $('#dvuserword').load(blog_username + 'message/GetUserWantSay', function (say) {
        $(this).empty().html(say);
        doUeParse();
    });
    getMess(1);
    $('#btnok').click(function () {
        if (!layerCheckLogin(true)) {
            return;
        }
        if (!ue.hasContents()) {
            layerAlert($("#hID").val() == "0" ? '你想对我说什么呢？' : "回复内容还没写呢！", function () { ue.focus(); });
            return;
        }
        var url;
        var prm;
        if ($("#hID").val() == "0") {
            url = blog_username + "message/sendmessage";
            prm = { content: $.htmlEncode(ue.getContent()) };
        } else {
            url = blog_username + "message/ReplyMsg";
            prm = { id: $("#hID").val(), content: $.htmlEncode(ue.getContent()) }
        }
        $.post(url, prm, function (o) {
            if (o.State == 1) {
                layerMsg($("#hID").val() == "0" ? '留言成功啦！' : '回复成功！');
                changeCount(1);
                getMess(1);
                clear();
            } else {
                layerAlert('操作失败：' + o.Message);
            }
        }, "json");
    });
});

function changeCount(count) {
    $('#spcount').html(parseInt($('#spcount').html()) + count);
    var count = parseInt($('#spcount').html());
    $('#hpage').val(count % 15 == 0 ? (count / 15) : (count / 15 + 1));
}

function flodReply(o) {
    if ($(o).text() == '折叠') {
        $(o).text('展开');
        $(o).parent().parent().nextAll('div').hide(500);
    } else {
        $(o).text('折叠');
        $(o).parent().parent().nextAll('div').show(500);
    }
}

function agree(id, count, a) {
    doAgree(blog_username + 'message/Agree', id, count, a);
}

function agreeReply(id, count, a) {
    doAgree(blog_username + 'message/AgreeReply', id, count, a);
}

function getMess(curr) {
    page = curr;
    $('#dvmsglist').html("正在加载留言...");
    $.get(blog_username + 'message/_UserMessagePage', { page: curr, username: $('#huid').val() },
        function (html) {
            $('#dvmsglist').html(html);
            doUeParse();
            laypage({
                cont: $('#layerpage'), //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                pages: $('#hpage').val(), //通过后台拿到的总页数
                curr: curr || 1, //当前页
                skin: 'molv',
                jump: function (obj, first) { //触发分页后的回调
                    if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                        getMess(obj.curr);
                        doUeParse();
                    }
                }
            });
        }, "html");
}